package protectjava;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

@WebServlet(name = "HelloForm1")
public class HelloForm1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        为姓名和姓氏创建cookie
        Cookie name = new Cookie("name",
                URLEncoder.encode(request.getParameter("name"),"UTF-8"));
        Cookie url = new Cookie("url",
                request.getParameter("url"));

//        为kookie设置过期日期为24小时
        name.setMaxAge(60*60*24);
        url.setMaxAge(60*60*24);

//        在响应头中添加两个Cookie
        response.addCookie(name);
        response.addCookie(url);

//        设置响应类型内容
        response.setContentType("text/html:charset=utf-8");

        PrintWriter out = response.getWriter();
        String title = "设置titlle实例";
        String doctype = "<!DOCTYPE html>\n";
        out.print(doctype +
            "<html>\n" +
                "<heard><title>" + title + "</title></heard>\n" +
                "<body bgcolor = \"#f0f0f0\">" +
                "<h1 align =\"center\" >"+title+"</h1>\n" +
                "<ur>\n" +
                "<li><b>站点名</b>;"
                + request.getParameter("name") + "</li> " +
                "<li><b>站点 URL:</b>:"
                        + request.getParameter("url") + "\n</li>" +
                        "</ur>\n" +
                "</body>" +
                "</html>"
        );
    }
}
